using System.Collections;
using System.Collections.Generic;
using MyFrame.Event;
using Plugin.Tool;
using UnityEngine;
using static Entity;

public class PlayerAttackCheck : EntityAttackCheck
{
    private Player player;
    private Animator animator;

    protected override void Awake()
    {
        base.Awake();
        player = GetComponentInParent<Player>();
        animator = GetComponentInParent<Animator>();
    }

    //拼刀
    private bool isKnife = false;
    // 避免弹反重复触发
    [HideInInspector] public bool isCounterAttackTrigger = false;

    //攻击和拼刀互斥     拼刀只产生一次，是玩家对敌人，当然双方都取消伤害
    protected override void OnTriggerEnter2D(Collider2D other)
    {
        //弹反，等待敌人进入攻击范围，然后进行重击
        if (player.isCounterAttack && !isCounterAttackTrigger)
        {
            isCounterAttackTrigger = true;

            //爆破特效
            GenerateHitEffect(other);
            //屏幕效果
            CameraMgr.Instance.TriggerScreenShake(player.LightShakePower);
            //顿帧
            CameraMgr.Instance.TriggerTimeFreeze(player.stats.lightFreaze);
            this.Emit<SoundName>(EventEnum.PlayerSound, SoundName.NormalSwordLightAttack1);

            //todo拉近相机

            // 这里等的是反击动画
            AnimationTool.AwaitAnimEnd(animator, () =>
            {
                player.CounterAttack();

                // 这里等的是反击结束动画  注意正在切换动画
                AnimationTool.AwaitCurrentAnimWhenEnd(animator, () =>
                {
                    player.CounterBackIdle();
                    isCounterAttackTrigger = false;
                });

            });
            return;
        }

        // 如果是武器，则拼刀 
        if (other.gameObject.layer == LayerMask.NameToLayer("Weapon"))
        {
            isKnife = true;
            ZTimer.SetTimeout(0.1f, () => isKnife = false);

            GenerateHitEffect(other);

            //屏幕效果
            CameraMgr.Instance.TriggerScreenShake(player.LightShakePower);
            //顿帧
            CameraMgr.Instance.TriggerTimeFreeze(player.stats.lightFreaze);
            this.Emit<SoundName>(EventEnum.PlayerSound, SoundName.SwordKnife);

            //x轴击退
            var backForce = new Vector2(player.stats.knockBackForce.x * 2f, 0);
            other.GetComponentInParent<Enemy>().BeBattleBack(player.stats.transform, backForce, player.stats.knockBackPower.Value);

            //todo拼刀成功，削减对方韧性

            // GetComponent<Collider2D>().enabled = false;
            return;
        }

        // 拼刀或弹反 就不触发伤害判断
        if (!player.isCounterAttack && !isKnife)
        {
            base.OnTriggerEnter2D(other);
        }
    }

    /// <summary>
    /// 产生特效
    /// </summary>
    void GenerateHitEffect(Collider2D other)
    {
        Vector3 edgePoint = Tool.GetCollider2DEdgePoint(transform.GetComponent<CapsuleCollider2D>(), player.facingRight);
        var hitPosition = other.ClosestPoint(edgePoint);
        Tool.GetEffectRandomPosAndRotation(player, hitPosition, out Vector3 randomPosition, out Quaternion effectRotation);
        EffectMgr.Instance.AddWorldEffect(HitEffect.NormalExplode, randomPosition, effectRotation, scale: 1.5f);
    }
}
